package com.sgg.gulimall_third_part.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.sgg.gulimall_third_part.service.OssService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author fileworld
 * @create 2021/9/30 0030
 * @project base_spring_cloud
 * @description
 */
@Service
public class OssServiceImpl implements OssService {

  @Value("${spring.cloud.alicloud.broker}")
  private String bucketName;

  @Value("${spring.cloud.alicloud.access-key}")
  private String accessKeyId;

  @Value("${spring.cloud.alicloud.oss.endpoint}")
  private String endpoint;

  @Value("${spring.cloud.alicloud.dir.prefix}")
  private String dir;

  @Autowired private OSS ossClient;

  /**
   * 服务端签名
   *
   * @return
   */
  @Override
  public Map<String, String> Signature() {

    Map<String, String> respMap = new LinkedHashMap<String, String>();
    try {
      long expireTime = 30;
      long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
      Date expiration = new Date(expireEndTime);
      // PostObject请求最大可支持的文件大小为5 GB，即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
      PolicyConditions policyConds = new PolicyConditions();
      policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
      policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
      String action = "http://" + bucketName + "." + endpoint;
      String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
      byte[] binaryData = postPolicy.getBytes("utf-8");
      String encodedPolicy = BinaryUtil.toBase64String(binaryData);
      String postSignature = ossClient.calculatePostSignature(postPolicy);

      respMap.put("accessKeyId", accessKeyId);
      respMap.put("policy", encodedPolicy);
      respMap.put("signature", postSignature);
      respMap.put("dir", dir);
      respMap.put("host", action);
      respMap.put("expire", String.valueOf(expireEndTime / 1000));


      //回调函数



      // respMap.put("expire", formatISO8601Date(expiration));
      //
      //      JSONObject jasonCallback = new JSONObject();
      //      jasonCallback.put("callbackUrl", callbackUrl);
      //      jasonCallback.put(
      //          "callbackBody",
      //
      // "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
      //      jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded");
      //      String base64CallbackBody =
      // BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
      //      respMap.put("callback", base64CallbackBody);
      //
      //      JSONObject ja1 = JSONObject.fromObject(respMap);
      //      // System.out.println(ja1.toString());
      //      response.setHeader("Access-Control-Allow-Origin", "*");
      //      response.setHeader("Access-Control-Allow-Methods", "GET, POST");
      //      response(request, response, ja1.toString());

    } catch (Exception e) {
      // Assert.fail(e.getMessage());
      System.out.println(e.getMessage());
    } finally {
      ossClient.shutdown();
    }
    return respMap;
  }
}
